package com.jlnk.androidfmc.view;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.jlnk.androidfmc.R;
import com.jlnk.androidfmc.fragment.SellerFragment;
import com.jlnk.androidfmc.fragment.fragments.BuyerHomeFragment;
import com.jlnk.androidfmc.fragment.fragments.FarmerCircleFragment;
import com.jlnk.androidfmc.fragment.fragments.MessageFragment;
import com.jlnk.androidfmc.fragment.fragments.ProfileFragment;
import com.jlnk.androidfmc.fragment.fragments.SchoolFragment;
import com.jlnk.androidfmc.fragment.fragments.SellerHomeFragment;

public class SellerFooterView extends RelativeLayout implements View.OnClickListener {

    private int[] tabIconsUnselected = {
            R.drawable.home, // 首页未选中
            R.drawable.friendcircle, // 农友圈未选中
            R.drawable.release, // 发布未选中
            R.drawable.message, // 消息未选中
            R.drawable.profile  // 我的未选中
    };
    private int[] tabIconsSelected = {
            R.drawable.home_select, // 首页选中
            R.drawable.friendcircle_select, // 农友圈选中
            R.drawable.release, // 发布选中
            R.drawable.message_select, // 消息选中
            R.drawable.profile_select  // 我的选中
    };

    private LinearLayout mTabHome;
    private LinearLayout mTabFarmerCircle;
    private LinearLayout mTabRelease;
    private LinearLayout mTabMessage;
    private LinearLayout mTabProfile;
    private SellerHomeFragment mSellerHomeFragment;
    private SchoolFragment mFarmerCircleFragment;
    private MessageFragment mMessageFragment;
    private ProfileFragment mProfileFragment;
    private Fragment mCurrentFragment;
    private FragmentManager mFragmentManager;

    public SellerFooterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.seller_footer_view, this, true);

        mTabHome = findViewById(R.id.home);
        mTabFarmerCircle = findViewById(R.id.farmercircle);
        mTabRelease = findViewById(R.id.release);
        mTabMessage = findViewById(R.id.message);
        mTabProfile = findViewById(R.id.profile);

        mSellerHomeFragment = new SellerHomeFragment();
        mFarmerCircleFragment = new SchoolFragment();
        mMessageFragment = new MessageFragment();
        mProfileFragment = new ProfileFragment();

        mTabHome.setOnClickListener(this);
        mTabFarmerCircle.setOnClickListener(this);
        mTabRelease.setOnClickListener(this);
        mTabMessage.setOnClickListener(this);
        mTabProfile.setOnClickListener(this);
    }

    public void setupWithFragmentManager(FragmentManager fragmentManager) {
        mFragmentManager = fragmentManager;
        displayFragment(mSellerHomeFragment);
        setTabSelected(mTabHome);
    }

    public void displayFragment(Fragment fragment) {
        if (mCurrentFragment != fragment) {
            mFragmentManager.beginTransaction()
                    .replace(R.id.fragment_container, fragment)
                    .commit();
            mCurrentFragment = fragment;
        }
    }


    private void resetTabs() {
        mTabHome.setSelected(false);
        mTabFarmerCircle.setSelected(false);
        mTabRelease.setSelected(false);
        mTabMessage.setSelected(false);
        mTabProfile.setSelected(false);
    }

    private void setTabSelected(LinearLayout tab) {
        // 重置所有标签的选中状态
        resetTabs();
        // 设置当前标签为选中状态
        tab.setSelected(true);
        // 更新所有底部导航栏图标
        updateTabIcons();
    }

    private void updateTabIcons() {
        // 更新所有底部导航栏图标
        updateIcon(mTabHome, tabIconsSelected[0], tabIconsUnselected[0]);
        updateIcon(mTabFarmerCircle, tabIconsSelected[1], tabIconsUnselected[1]);
        // 发布按钮不需要变换图标
        updateIcon(mTabMessage, tabIconsSelected[3], tabIconsUnselected[3]);
        updateIcon(mTabProfile, tabIconsSelected[4], tabIconsUnselected[4]);
    }

    private void updateIcon(LinearLayout tab, int selectedIcon, int unselectedIcon) {
        ImageView iconImageView;
        switch (tab.getId()) {
            case R.id.home:
                iconImageView = tab.findViewById(R.id.imageHome);
                break;
            case R.id.farmercircle:
                iconImageView = tab.findViewById(R.id.imageFarmerCircle);
                break;
            case R.id.message:
                iconImageView = tab.findViewById(R.id.imageMessage);
                break;
            case R.id.profile:
                iconImageView = tab.findViewById(R.id.imageProfile);
                break;
            default:
                iconImageView = null; // 不应该发生，因为只有四个选项
        }
        if (iconImageView != null) {
            iconImageView.setImageResource(tab.isSelected() ? selectedIcon : unselectedIcon);
        }
    }

    @Override
    public void onClick(View v) {
        Fragment newFragment = null;
        LinearLayout newTab = null;
        switch (v.getId()) {
            case R.id.home:
                newFragment = mSellerHomeFragment;
                newTab = mTabHome;
                break;
            case R.id.farmercircle:
                displayFragment(new FarmerCircleFragment());
                setTabSelected(mTabFarmerCircle);
                break;
            case R.id.release:
                // 发布跳转逻辑
                Intent intent = new Intent(getContext(), ReleaseActivity.class);
                getContext().startActivity(intent);
                return; // 返回，防止执行后面的代码
            case R.id.message:
                newFragment = mMessageFragment;
                newTab = mTabMessage;
                break;
            case R.id.profile:
                newFragment = mProfileFragment;
                newTab = mTabProfile;
                break;
        }
        if (newFragment != null) {
            displayFragment(newFragment);
            setTabSelected(newTab);
        }
    }

}